package com.cloudcc.mobile.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;

import java.lang.reflect.Field;
import java.util.List;


/**
 * Created by Administrator on 2017/10/12.
 */

public class RecyGridAdapter extends RecyclerView.Adapter<RecyGridAdapter.MainuiViewHolder>{
    private Context mContext;
    private List<MenuTabModel> currentShowList;

    public RecyGridAdapter(Context mContext, List<MenuTabModel> currentShowList) {
        this.mContext = mContext;
        this.currentShowList = currentShowList;
    }
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    @Override
    public MainuiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        MainuiViewHolder holder = new MainuiViewHolder(LayoutInflater.from(mContext ).inflate(R.layout.menu_add_item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MainuiViewHolder holder,final int position) {
        if(mOnItemClickLitener!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos= holder.getPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });
        }
        holder.name.setText(currentShowList.get(position).tab_name);
        if("android:approval".equals(currentShowList.get(position).objId)){
//			Log.d("hahahahahah", t.tabName+"::"+t.objId);
            String o = RunTimeManager.getInstance().getDaiNum();
            if(null!=RunTimeManager.getInstance().getDaiNum()&&(!"0".equals(RunTimeManager.getInstance().getDaiNum()))&&(""!=RunTimeManager.getInstance().getDaiNum())){
                holder.dropptext.setVisibility(View.VISIBLE);
                holder.dropptext.setText(RunTimeManager.getInstance().getDaiNum());
            }else{
                holder.dropptext.setVisibility(View.INVISIBLE);
            }
        }else{
           holder.dropptext.setVisibility(View.INVISIBLE);
        }


        try {
          if(currentShowList.get(position).objId.startsWith("android:addmore")){
              holder.icon.setImageResource(R.drawable.main_cord_addmore);
              return;
          }
            Class drawable = R.drawable.class;
            Field field = drawable.getField(currentShowList.get(position).imageId);
            int id = field.getInt(field.getName());
            if(id>0){
                holder.icon.setImageResource(id);
            }else{
                holder.icon.setImageResource(R.drawable.cloudtab_weixsld);
            }

        } catch (Exception e) {
            e.printStackTrace();
            holder.icon.setImageResource(R.drawable.cloudtab_weixsld);
        }


    }

    @Override
    public int getItemCount() {
        return currentShowList.size();
    }

    class MainuiViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        ImageView icon;
        TextView dropptext;
        public MainuiViewHolder(View itemView) {
            super(itemView);
             name = (TextView) itemView.findViewById(R.id.menu_add_name);
            icon = (ImageView) itemView.findViewById(R.id.menu_add_icon);
            dropptext = (TextView) itemView.findViewById(R.id.dropptext);
        }
    }
    public void changeRecycleData(List<MenuTabModel> data) {
        if (currentShowList != null) {
            currentShowList.clear();
            currentShowList = null;
        }
        currentShowList = data;
//        Log.d("isnull", "is:" + mData != null + "::" + data.toString() + "::"
//                + (data != null));
//        conut = 0;
        notifyDataSetChanged();

    }
}
